package vn.edu.uit.wifi_transfering;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBConnection {
	private static final String KEY_ROWID = "_id";
	private static final String KEY_HOST = "host";
	private static final String KEY_PORT = "port";
	private static final String KEY_USERNAME = "username";
	private static final String KEY_PASSWORD = "password";
	private static final String KEY_ANONYMOUS = "anonymous";
	private static final String TAG = "DBAdapter";

	private static final String DATABASE_NAME = "MyListServerSocket";
	private static final String DATABASE_TABLE = "servers";
	private static final int DATABASE_VERSION = 1;

	private static final String DATABASE_CREATE = "create table servers (_id integer primary key autoincrement, "
			+ "host vachar(12) not null, port integer not null, "
			+ "username vachar(50), password vachar(50), anonymous integer not null);";

	private final Context context;
	private DatabaseHelper DBHelper;
	private SQLiteDatabase db;

	public DBConnection(Context ctx) {
		this.context = ctx;
		DBHelper = new DatabaseHelper(context);
	}

	private static class DatabaseHelper extends SQLiteOpenHelper {

		public DatabaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			// TODO Auto-generated method stub
			try {
				db.execSQL(DATABASE_CREATE);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			// TODO Auto-generated method stub
			Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
					+ newVersion + ", which will destroy all old data");
			db.execSQL("DROP TABLE IF EXISTS servers");
			onCreate(db);
		}
	}

	// ---opens the database---
	public DBConnection open() throws SQLException {
		db = DBHelper.getWritableDatabase();
		return this;
	}

	// ---closes the databse---
	public void close() {
		DBHelper.close();
	}

	// ---insert a server into the database---
	public long insertServer(String host, int port, String user, String pass, int anonymous) {
		ContentValues initialValues = new ContentValues();
		initialValues.put(KEY_HOST, host);
		initialValues.put(KEY_PORT, port);
		initialValues.put(KEY_USERNAME, user);
		initialValues.put(KEY_PASSWORD, pass);
		initialValues.put(KEY_ANONYMOUS, anonymous);
		return db.insert(DATABASE_TABLE, null, initialValues);
	}

	// ---delete a particular server---
	public boolean deleteServer(long rowId) {
		return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
	}

	// ---delete all servers---
	public boolean deleteAllServers(){
		return db.delete(DATABASE_TABLE, "1", null) > 0;
	}
	
	// ---retrieves all the servers---
	public Cursor getAllservers() {
		return db.query(DATABASE_TABLE, new String[] { KEY_ROWID, KEY_HOST,
				KEY_PORT, KEY_USERNAME, KEY_PASSWORD, KEY_ANONYMOUS }, null, null, null, null, null);
	}

	// ---retrieves a particular server---
	public Cursor getServer(long rowId) throws SQLException {
		Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] {
				KEY_ROWID, KEY_HOST, KEY_PORT, KEY_USERNAME, KEY_PASSWORD, KEY_ANONYMOUS }, KEY_ROWID + "=" + rowId,
				null, null, null, null, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;
	}
	
	//---upgrades a server---
	public boolean updateServer(long rowId, String host, int port, String user, String pass, int anonymous){
		ContentValues args = new ContentValues();
		args.put(KEY_HOST, host);
		args.put(KEY_PORT, port);
		args.put(KEY_USERNAME, user);
		args.put(KEY_PASSWORD, pass);
		args.put(KEY_ANONYMOUS, anonymous);
		return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
	}

}
